/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software Foundation,
 *    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package JET.MouseStuff.SpecialTasks;

import JET.StaticActions;
import JET.Centers.ControlCenter;
import JET.Frames.DataModels.DataModelsFactory;
import JET.MouseStuff.Task;
import JET.SA.SA_Mass;
import JET.SA.SA_SpringBody;
import JET.SA.SA_Wall;
import JET.Scene;
import JET.SceneHelper;
import JET.physics.Mass;
import JET.physics.Spring;
import JET.physics.bodies.SpringBody;
import JET.physics.Wall;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author 057Ry
 */
public class JoinSpringBodiesByMassesTask implements Task
{
    static final ControlCenter CC = ControlCenter.getInstance();
    private Scene scene;

    public JoinSpringBodiesByMassesTask(Scene s) {
        scene = s;
    }

    public boolean needKeyConfirmationToProceed() {
        return false;
    }

    public int getKeyCode() {
        return 0;
    }

    public boolean canProceed() {
        return true;
    }

    public synchronized void execute()
    {
        ArrayList<SpringBody> sbList1 = new ArrayList<SpringBody>();

        Collection<Mass> massCol1 = scene.EC.savedSelectionGroup1.getMassListSync();
        synchronized(massCol1) {
            for(Mass mass : massCol1)
            {
                SpringBody sb = SceneHelper.getSpringBodyThatOwnMass(scene, mass);
                if ( !sbList1.contains(sb) )
                    sbList1.add(sb);
            }
        }

        Collection<Mass> massCol2 = scene.EC.savedSelectionGroup2.getMassListSync();
        synchronized(massCol2) {
            for(Mass mass : massCol2)
            {
                SpringBody sb = SceneHelper.getSpringBodyThatOwnMass(scene, mass);
                if ( !sbList1.contains(sb) )
                    sbList1.add(sb);
            }
        }

        SpringBody newSB = new SpringBody();
        for(SpringBody sb : sbList1)
        {
            newSB.addMasses( sb.getMassList() );
            newSB.addSprings( sb.getSpringList() );
            newSB.addWallsToBorder( sb.getBorder().getWallList() );
            SA_SpringBody.removeSpringBody(sb,scene);
        }
        
        SA_SpringBody.addSpringBodyToScene(newSB, scene, true);

        // Wywalenie nadmiarowych scian

        for(int i=0; i<scene.EC.savedSelectionGroup1.getMassList().size()-1; i++)
        {
            Mass mass1 = scene.EC.savedSelectionGroup1.getMassList().get(i);
            Mass mass2 = scene.EC.savedSelectionGroup1.getMassList().get(i+1);

            for(int j=0; j<newSB.getBorder().getWallsCount(); j++)
            {
                Wall wall = newSB.getBorder().getWallList().get(j);
                if ( (wall.getM1().equals(mass1) && wall.getM2().equals(mass2)) ||
                     (wall.getM1().equals(mass2) && wall.getM2().equals(mass1)) )
                    SA_Wall.removeWallFromSpringBody(wall, scene, false);
            }
        }
        for(int i=0; i<scene.EC.savedSelectionGroup2.getMassList().size()-1; i++)
        {
            Mass mass1 = scene.EC.savedSelectionGroup2.getMassList().get(i);
            Mass mass2 = scene.EC.savedSelectionGroup2.getMassList().get(i+1);

            for(int j=0; j<newSB.getBorder().getWallsCount(); j++)
            {
                Wall wall = newSB.getBorder().getWallList().get(j);
                if ( (wall.getM1().equals(mass1) && wall.getM2().equals(mass2)) ||
                     (wall.getM1().equals(mass2) && wall.getM2().equals(mass1)) )
                    SA_Wall.removeWallFromSpringBody(wall, scene, false);
            }
        }

        // Podmienianie mass
        for(int i=0; i<scene.EC.savedSelectionGroup1.getMassList().size(); i++)
        {
            if (scene.EC.savedSelectionGroup2.getMassList().size() == i) break;
            Mass mass1 = scene.EC.savedSelectionGroup1.getMassList().get(i);
            Mass mass2 = scene.EC.savedSelectionGroup2.getMassList().get(i);
            ArrayList<Spring> sl = SceneHelper.getSpringListOfMass(scene, mass1, true, true, true);
            ArrayList<Wall> wl = SceneHelper.getWallListOfMass(scene, mass1);

            for(Spring spring : sl)
                if (spring.getM1().equals(mass1))
                    spring.setM1( mass2 );
                else if (spring.getM2().equals(mass1))
                    spring.setM2( mass2 );
                
            for(Wall wall : wl)
                if (wall.getM1().equals(mass1))
                    wall.setM1( mass2 );
                else if (wall.getM2().equals(mass1))
                    wall.setM2( mass2 );
            
            SA_Mass.removeMassFromSpringBody(mass1, scene, false);
        }
        System.out.println("bodies joined...");
        scene.EC.selectionGroup.clear();
        scene.EC.savedSelectionGroup1.clear();
        scene.EC.savedSelectionGroup2.clear();

        DataModelsFactory.getSceneTreeModel().fireWholeTreeChanged();
   }
}